package com.quanshi.cloudmeeting.ci.assistant;

import java.awt.geom.QuadCurve2D;
import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.apache.commons.io.FileUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ReleaseNoteMaker {
	private static final String UTF_8 = "UTF-8";
	private static final String LINE_SEPARATOR = System.getProperty("line.separator", "\n");
	
	public boolean genReleaseNoteFromChangeLog(String changelogPath,
			String targetPath) throws Exception {
		System.out.println("woking on releasesnote....................");
		// build doc
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		factory.setNamespaceAware(true);
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document doc = builder.parse(changelogPath);

		// build xpath factory
		String xpathStr = "//msg";
		XPathFactory xpathfactory = XPathFactory.newInstance();
		XPath xpath = xpathfactory.newXPath();
		XPathExpression expr = xpath.compile(xpathStr);

		// parse
		Object result = expr.evaluate(doc, XPathConstants.NODESET);
		NodeList nodes = (NodeList) result;
		StringBuffer buffer = new StringBuffer();
		int j = 0;
		for (int i = 0; i < nodes.getLength(); i++) {
			Node n = nodes.item(i);
			String msg = n.getTextContent();
			if (msg != null && msg.trim().length() > 0) {
				j++;
				msg = j + "." + msg
						+ System.getProperty("line.separator", "\n");
				buffer.append(msg);
			}
		}
		FileUtils.write(new File(targetPath), buffer.toString());
		System.out.println("Generate " + targetPath + " successfully!");
		return true;
	}

	public static void main(String[] args) throws Exception {
		
		//0 changelog.xml path
		//1 releasenote.txt path
		//2 publish androidimage url
		//3 publish iosimage url
		//4 local android image path
		//5 local ios image path
		//6 target image txt path
		//7 version
		//8 publish url
		//9 contenttemplate path
		//10 contenttxt targetpath
		//11 version targepath 
		//12 image targetpath
		if (args == null || args.length == 0 || args.length < 9) {
			System.out.println("Change log path and target releasenotes path can not be empty");
			return;
		}
		int i=0;
		for(String value:args){
	
			System.out.println(i++ +":"+value);
		}

		String appStr = args[8];
		String[] arrayParam = appStr.split("##");
		
		for(String value:arrayParam){
			System.out.println(i++ +":"+value);
		}
		
		
		//generate releasenotes
		String sourceFilePath = args[0];
		String targetPath = args[1];
 
		ReleaseNoteMaker maker = new ReleaseNoteMaker();
		maker.genReleaseNoteFromChangeLog(sourceFilePath, targetPath);
		
		//generate image
		Qrcode qrcode = new Qrcode();
		qrcode.MakeImage(args[2], args[4]);
		qrcode.MakeImage(args[3], args[5]);
		
		
		//generate version.txt
		maker.makeVerstionTxt(args[7], arrayParam[3]);
		
		//generate image.txt
		maker.makeQrCodeImageUrlTxt(arrayParam[0]+"/android.jpg",arrayParam[0]+"/ios.jpg",arrayParam[4]);
		
		
		//generate content.txt
		maker.makeMailContextTxt(arrayParam[1], FileUtils.readFileToString(FileUtils.getFile(targetPath)), args[7],  arrayParam[0], arrayParam[2]);
		

	}

	public void makeVerstionTxt(String version, String path) throws Exception {
		// TODO Auto-generated method stub
		File file = new File(path);
		FileUtils.write(file, version, false);
	}

	public void makeQrCodeImageUrlTxt(String imageUrlandroid,
			String imageUrlIos, String targetImageTxt) throws IOException {
		// TODO Auto-generated method stub
		String template = "<br>android" + "<br>"
				+ "<img src=\"$androidimage\"/>" + "<br>" + "<br>" + "<br>"
				+ "ios" + "<br>" + "<img src=\"$iosimage\"/>";

		String imageTxtContext = template.replace("$androidimage",
				imageUrlandroid).replace("$iosimage", imageUrlIos);
		FileUtils.write(new File(targetImageTxt), imageTxtContext, false);

	}

	public void makeMailContextTxt(String mailContextTxtTemplate,
			String changelog, String version, String publishpath,
			String targetcontextpath) throws Exception {
		
		String templateStr = FileUtils.readFileToString(FileUtils.getFile(mailContextTxtTemplate),UTF_8);
		templateStr = templateStr.replace("$version", version)
				.replace("$publish_path",publishpath)
				.replace("$changelog", changelog)
				.replace(LINE_SEPARATOR, LINE_SEPARATOR+"<br>");
		
		FileUtils.write(new File(targetcontextpath), templateStr,UTF_8);
		// TODO Auto-generated method stub
		
	}
}
